//
//  CustomSegmentControl.m
//  SwipeTableView
//
//  Created by Roy lee on 16/5/28.
//  Copyright © 2016年 Roy lee. All rights reserved.
//

#import "CustomSegmentControl.h"
#import "UIView+STFrame.h"
#define RGBColor(r,g,b)     [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]

@interface CustomSegmentControl ()

@property (nonatomic, strong) UIView * contentView;
@property (nonatomic, strong) NSArray * items;

@property (nonatomic,strong)UIView * viewBlcak;//黑色滑块

@end

@implementation CustomSegmentControl

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
        BaseView *view = [[BaseView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        view.backgroundColor = [UIColor RGBColorWithR:238 g:238 b:238];
        [self addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(-2);
            make.left.right.equalTo(self).offset(0);
            make.height.mas_equalTo(2);
        }];
        [self addSubview:self.viewBlcak];

    }
    return self;
}

- (instancetype)initWithItems:(NSArray *)items {
    self = [self initWithFrame:CGRectZero];
    if (self) {
        if (items.count > 0) {
            self.items = items;
        }
    }
    return self;
}

- (void)commonInit {
    _contentView = [UIView new];
    _contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_contentView];
    _font = [UIFont systemFontOfSize:20];
    _textColor = [UIColor blueColor];
    _selectedTextColor = [UIColor redColor];
    _selectionIndicatorColor = RGBColor(150, 150, 150);
    _items = @[@"Segment0",@"Segment1"];
    _selectedSegmentIndex = 0;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    for (UIView *subView in _contentView.subviews) {
        [subView removeFromSuperview];
    }
    
    _contentView.backgroundColor = _backgroundColor;
    _contentView.frame = self.bounds;
    for (int i = 0; i < _items.count; i ++) {
        UIButton * itemBt = [UIButton buttonWithType:UIButtonTypeCustom];
        itemBt.tag = 666 + i;
        [itemBt setTitleColor:_textColor forState:UIControlStateNormal];
        [itemBt setTitleColor:_selectedTextColor forState:UIControlStateSelected];
        [itemBt setTitle:_items[i] forState:UIControlStateNormal];
        
        if (i == 0) {
            /** V4_新增加 */
            itemBt.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];
        }else{
            itemBt.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:15];
        }
     
        CGFloat itemWidth = self.st_width/_items.count;
        itemBt.st_size = CGSizeMake(itemWidth, self.st_height);
        itemBt.st_x    = itemWidth * i;
        if (i == _selectedSegmentIndex) {
            itemBt.backgroundColor = _selectionIndicatorColor;
            itemBt.selected = YES;
        }else {
            itemBt.backgroundColor = [UIColor clearColor];
        }
        [itemBt addTarget:self action:@selector(didSelectedSegment:) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:itemBt];
    }
}

- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex {
    NSInteger index = selectedSegmentIndex;
    UIButton * oldItemBt      = [_contentView viewWithTag:666 + _selectedSegmentIndex];
    oldItemBt.backgroundColor = [UIColor clearColor];
    oldItemBt.selected        = NO;
    
    UIButton * itemBt      = [_contentView viewWithTag:666 + selectedSegmentIndex];
    itemBt.backgroundColor = _selectionIndicatorColor;
    itemBt.selected        = YES;
    _selectedSegmentIndex  = selectedSegmentIndex;

    
    /** V4_新增加 */
    oldItemBt.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];
    
    itemBt.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:15];
    
    [self chageVBlackV];
}


-(void)chageVBlackV{
    UIButton * itemBt = [_contentView viewWithTag:666 + _selectedSegmentIndex];

    CGRect rect = CGRectMake(0,36, kScreenW/2.0, 3);
    
    if (_selectedSegmentIndex == 0) {
        rect.origin.x = 0;
    }else{
        rect.origin.x = kScreenW/2.0;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.viewBlcak.frame = rect;
    }];
}

- (void)didSelectedSegment:(UIButton *)itemBt {
    UIButton * oldItemBt      = [_contentView viewWithTag:666 + _selectedSegmentIndex];
    oldItemBt.backgroundColor = [UIColor clearColor];
    oldItemBt.selected        = NO;
    

    itemBt.backgroundColor = _selectionIndicatorColor;
    itemBt.selected        = YES;
    _selectedSegmentIndex  = itemBt.tag - 666;
    if (self.IndexChangeBlock) {
        self.IndexChangeBlock(_selectedSegmentIndex);
    }
    [self sendActionsForControlEvents:UIControlEventValueChanged];
    
    
    /** V4_新增加 */
    itemBt.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];

    oldItemBt.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:15];


    [self chageVBlackV];

}

-(UIView *)viewBlcak{
    if (!_viewBlcak) {
        _viewBlcak = [[UIView alloc]initWithFrame:CGRectMake(0,36, kScreenW/2.0, 3)];
        _viewBlcak.backgroundColor = [UIColor blackColor];

    }
    return _viewBlcak;
}

@end





